import java.util.Stack;

/**
 * @program: 1.15 栈和队列
 * @description:
 * @author: 李鑫月
 * @create: 2022-01-16 22:57
 * 用两个栈实现队列
 **/
public class MyQueue1 {

    Stack<Integer> stack1;
    Stack<Integer> stack2;

    public MyQueue1() {
        stack1=new Stack<>();
        stack2=new Stack<>();

    }

    public void push(int x) {
        if(empty()){
            stack1.push(x);
        }
        else if(!stack1.empty()){
            stack1.push(x);
        }else
        {
            stack2.push(x);
        }
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(stack1.empty()){
            while(!stack2.empty()){
                stack1.push(stack2.peek());
                stack2.pop();
            }
            int ret=stack1.peek();
            stack1.pop();
            while(!stack1.empty()){
                stack2.push(stack1.peek());
                stack1.pop();
            }
            return ret;
        }else{
            while(!stack1.empty()){
                stack2.push(stack1.peek());
                stack1.pop();
            }
            int ret=stack2.peek();
            stack2.pop();
            while(!stack2.empty()){
                stack1.push(stack2.peek());
                stack2.pop();
            }
            return ret;
        }
    }

    public int peek() {
        if(empty()){
            return -1;
        }
        if(stack1.empty()){
            while(!stack2.empty()){
                stack1.push(stack2.peek());
                stack2.pop();
            }
            int ret=stack1.peek();
            while(!stack1.empty()){
                stack2.push(stack1.peek());
                stack1.pop();
            }




            return ret;
        }else{
            while(!stack1.empty()){
                stack2.push(stack1.peek());
                stack1.pop();
            }
            int ret=stack2.peek();
            while(!stack2.empty()){
                stack1.push(stack2.peek());
                stack2.pop();
            }
            return ret;
        }
    }

    public boolean empty() {
        return stack1.empty()&&stack2.empty();
    }
}



/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */